<?php
/**
 * ProjectName: Grapes.
 * Created by LemonZ.
 * FileName: AdminRole.php
 * Date: 2019/2/13
 * Time: 23:19
 * Description:
 */

namespace app\common\model;


use think\Db;

class AdminRole extends Common
{
    protected $autoWriteTimestamp = true;
    protected $updateTime = 'utime';

    protected function tableWhere($post)
    {
        $where = [];

        if(isset($post['name']) && $post['name'] != ""){
            $where[] = ['name', 'like', '%'.$post['name'].'%'];
        }
        $result['where'] = $where;
        $result['field'] = "*";
        $result['order'] = 'utime desc';
        return $result;
    }

    protected function tableFormat($list)
    {
        foreach($list as $k => $v) {
            if($v['utime']) {
                $list[$k]['utime'] = getTime($v['utime']);
            }
        }
        return $list;
    }

    public function toDel($id)
    {
        $result = [
            'status' => false,
            'data' => [],
            'msg' => ''
        ];

        $where['id'] = $id;

        $mrorModel = new AdminRoleOperationRel();

        Db::startTrans();
        try {
            $this->where($where)->delete();
            $mrorModel->where(['admin_role_id'=>$id])->delete();
            Db::commit();
            $result['status'] = true;
            $result['msg'] = '删除成功';
        } catch (\Exception $e) {
            Db::rollback();
            $result['msg'] = $e->getMessage();
        }
        return $result;
    }

    public function getRoleOperation($id)
    {
        $result = [
            'status' => true,
            'data'   => [],
            'msg'    => ''
        ];

        $where['id']    = $id;
        $sellerRoleInfo = $this->where($where)->find();
        if (!$sellerRoleInfo) {
            return error_code(11071);
        }
        $mrorModel = new AdminRoleOperationRel();
        $permList  = $mrorModel->where(['admin_role_id' => $id])->select();
        if (!$permList->isEmpty()) {
            $nodeList = array_column($permList->toArray(), 'admin_role_id', 'operation_id');
        } else {
            $nodeList = [];
        }

        $operationModel = new Operation();
        $result['data'] = $operationModel->menuTree($operationModel::MENU_ADMIN, $nodeList);
        return $result;
    }
}